//
//  wintelViewController.m
//  Secret
//
//  Created by 隋文涛 on 13-12-11.
//  Copyright (c) 2013年 隋文涛. All rights reserved.
//

#import "wintelViewController.h"

@interface wintelViewController ()

@end

@implementation wintelViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    CGRect rect_Button_SettingDone = self.Button_SettingDone.frame;
    rect_Button_SettingDone.origin.y = self.view.frame.size.height/2 - 70;
    self.Button_SettingDone.frame = rect_Button_SettingDone;
    
    CGRect rect_View_SettingUp = self.View_SettingUp.frame;
    rect_View_SettingUp.origin.y = -rect_View_SettingUp.size.height;
    self.View_SettingUp.frame = rect_View_SettingUp;
    CGRect rect_View_SettingDown = self.View_SettingDown.frame;
    rect_View_SettingDown.origin.y = self.view.frame.size.height;
    self.View_SettingDown.frame = rect_View_SettingDown;
    CGRect rect_View_Setting = self.View_Setting.frame;
    rect_View_Setting.origin.x = 0 - self.view.frame.size.width;
    self.View_Setting.frame = rect_View_Setting;
    
    [self Checkdata];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == _Textfield_InputPassword) {
        [_Textfield_InputPassword resignFirstResponder];
    }else if (textField == _Textfield_SetInputPassword_Old){
        [_Textfield_SetInputPassword_New01 becomeFirstResponder];
    }else if (textField == _Textfield_SetInputPassword_New01){
        [_Textfield_SetInputPassword_New02 becomeFirstResponder];
    }else if (textField == _Textfield_SetInputPassword_New02){
        [_Textfield_SetInputPassword_Old resignFirstResponder];
        [_Textfield_SetInputPassword_New01 resignFirstResponder];
        [_Textfield_SetInputPassword_New02 resignFirstResponder];
        
        [self Button_SettingDoneClicked:nil];
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@" "]) {
        return NO;
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == _Textfield_SetInputPassword_New01 || textField == _Textfield_SetInputPassword_New02){
        if (![_Textfield_SetInputPassword_New01.text isEqualToString:_Textfield_SetInputPassword_New02.text]) {
            _Label_SettingInfo.text = @"两次输入新密码不一致";
        }else{
            _Label_SettingInfo.text = @"两次输入新密码一致";
        }
    }
}



- (IBAction)textField_EditChanged:(id)sender {
    UITextField *textField = (UITextField *)sender;
    if (textField == _Textfield_SetInputPassword_New01 || textField == _Textfield_SetInputPassword_New02){
        if (![_Textfield_SetInputPassword_New01.text isEqualToString:_Textfield_SetInputPassword_New02.text]) {
            _Label_SettingInfo.text = @"两次输入新密码不一致";
        }else{
            _Label_SettingInfo.text = @"两次输入新密码一致";
            NSLog(@"%@",[PublicClass GOTMD5PWwithstring:_Textfield_SetInputPassword_New02.text]);
        }
    }else if (textField == _Textfield_SetInputPassword_Old){
        if ([[PublicClass GOTMD5PWwithstring:_Textfield_SetInputPassword_Old.text] isEqualToString:password]) {
            _Label_SettingOldPasswordInfo.text = @"正确";
        }else{
            _Label_SettingOldPasswordInfo.text = @"错误";
        }
    }else if(textField == _Textfield_InputPassword){
        if ([[PublicClass GOTMD5PWwithstring:_Textfield_InputPassword.text] isEqualToString:password]) {
            _Label_TextFieldInfo.text = @"密码输入正确";
            [self animate];
            [self performSelector:@selector(TrunToPage) withObject:nil afterDelay:1];
        }else{
            _Label_TextFieldInfo.text = @"密码输入错误";
        }
    }
}

- (void)TrunToPage{
    UITabBarController *TB = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeUITabBarController"];
    [self presentViewController:TB animated:YES completion:nil];
}


/**
 *  segue方法
 *
 *  @param IBAction
 *
 *  @return
 */
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    //页面跳转
    if (1) {
        NSLog(@"");
        NSLog(@"");
        
    }
}

- (void)animate{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        self.ImageView_ViewUp.transform = CGAffineTransformMakeRotation(M_PI/2);
        self.ImageView_ViewDown.transform = CGAffineTransformMakeRotation(M_PI/2);
    } completion:^(BOOL finished) {}];
    
    [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        CGRect rectup = self.View_Up.frame;
        rectup.origin.y = 0 - rectup.size.height;
        self.View_Up.frame = rectup;
        
        CGRect rectInputBase = self.View_InputBase.frame;
        rectInputBase.origin.y = 0 - rectup.size.height;
        self.View_InputBase.frame = rectInputBase;
        
        CGRect rectdown = self.View_Down.frame;
        rectdown.origin.y = self.view.frame.size.height;
        self.View_Down.frame = rectdown;
    } completion:^(BOOL finished) {}];
}


- (IBAction)BackgroundClicked:(id)sender {
    [_Textfield_InputPassword resignFirstResponder];
}

- (IBAction)Button_SettingShowPasswordTouchDown:(id)sender {
    [_Textfield_SetInputPassword_New01 setSecureTextEntry:NO];
    [_Textfield_SetInputPassword_New02 setSecureTextEntry:NO];
}

- (IBAction)Button_SettingShowPasswordTouchCancel:(id)sender {
    [_Textfield_SetInputPassword_New01 setSecureTextEntry:YES];
    [_Textfield_SetInputPassword_New02 setSecureTextEntry:YES];
}

- (IBAction)Button_SettingDoneClicked:(id)sender {
    if ([_Textfield_SetInputPassword_Old isHidden]) {
        if (![_Textfield_SetInputPassword_New01.text isEqualToString:_Textfield_SetInputPassword_New02.text]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"两次输入的密码不一致" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }else{
            if (_Textfield_SetInputPassword_New01.text.length < 5) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入一个超过5位的密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }else{
                [self SuccessData];
            }
        }
    }else{
        if ([[PublicClass GOTMD5PWwithstring:_Textfield_SetInputPassword_Old.text] isEqualToString:password]) {
            if (![_Textfield_SetInputPassword_New01.text isEqualToString:_Textfield_SetInputPassword_New02.text]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"两次输入的密码不一致" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }else{
                if (_Textfield_SetInputPassword_New01.text.length < 5) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入一个超过4位的密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alert show];
                }else{
                    [self SuccessData];
                }
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"输入的原密码错误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
    }
}

- (void)SuccessData{
    NSString *NewPassword = self.Textfield_SetInputPassword_New01.text;
    NSString *NewPasswordMD5 = [PublicClass GOTMD5PWwithstring:NewPassword];
    [PublicClass setgetPASSWORDValue:NewPasswordMD5];
    [_Textfield_SetInputPassword_Old resignFirstResponder];
    [_Textfield_SetInputPassword_New01 resignFirstResponder];
    [_Textfield_SetInputPassword_New02 resignFirstResponder];
    [self performSelector:@selector(View_SettingShow:) withObject:@"0" afterDelay:0.3];
    
    password = [PublicClass getPASSWORD];
    [self performSelector:@selector(Checkdata) withObject:nil afterDelay:2];
}

- (void)Checkdata{
    [_Textfield_InputPassword resignFirstResponder];
    password = [PublicClass getPASSWORD];
    if (password!=nil && password>0) {
        [_Textfield_SetInputPassword_Old setHidden:NO];
        [_Label_SettingOldPasswordInfo setHidden:NO];
    }else{
        [_Textfield_SetInputPassword_Old setHidden:YES];
        [_Label_SettingOldPasswordInfo setHidden:YES];
        [self Button_SettingClicked:nil];
    }
}

- (IBAction)Button_SettingCancelCilcked:(id)sender {
    [_Textfield_SetInputPassword_Old resignFirstResponder];
    [_Textfield_SetInputPassword_New01 resignFirstResponder];
    [_Textfield_SetInputPassword_New02 resignFirstResponder];
    [self performSelector:@selector(View_SettingShow:) withObject:@"0" afterDelay:0.3];
    
    [self performSelector:@selector(Checkdata) withObject:nil afterDelay:2];
}

- (IBAction)Button_SettingClicked:(id)sender {
    [self View_SettingShow:@"1"];
}

- (void)View_SettingShow:(NSString *)show{
    if ([show isEqualToString:@"1"]) {
        [_View_Setting setHidden:NO];
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
                CGRect rect_View_Setting = self.View_Setting.frame;
                rect_View_Setting.origin.x = 0;
                self.View_Setting.frame = rect_View_Setting;
        } completion:^(BOOL finished) {
        }];
        [UIView animateWithDuration:1 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            CGRect rect_View_SettingUp = self.View_SettingUp.frame;
            rect_View_SettingUp.origin.y = 0;
            self.View_SettingUp.frame = rect_View_SettingUp;
            
            CGRect rect_View_SettingDown = self.View_SettingDown.frame;
            if (CurrentDeviceisIpad) {
                rect_View_SettingDown.origin.y = self.view.frame.size.height/2;
            }else{
                rect_View_SettingDown.origin.y = self.view.frame.size.height - 350;
            }
            self.View_SettingDown.frame = rect_View_SettingDown;
        } completion:^(BOOL finished) {}];
    }else{
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
                CGRect rect_View_SettingUp = self.View_SettingUp.frame;
                rect_View_SettingUp.origin.y = -rect_View_SettingUp.size.height;
                self.View_SettingUp.frame = rect_View_SettingUp;
                
                CGRect rect_View_SettingDown = self.View_SettingDown.frame;
                rect_View_SettingDown.origin.y = self.view.frame.size.height;
                self.View_SettingDown.frame = rect_View_SettingDown;
        } completion:^(BOOL finished) {
        }];
        [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            CGRect rect_View_Setting = self.View_Setting.frame;
            rect_View_Setting.origin.x = 0 - self.view.frame.size.width;
            self.View_Setting.frame = rect_View_Setting;
        } completion:^(BOOL finished) {
            [_View_Setting setHidden:YES];
        }];
    }
}
@end
